﻿#define _CRT_SECURE_NO_WARNINGS
#include"game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("--------扫雷游戏-------\n");
	for (int i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <=row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <=col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int coont = EASY_COUNT;
	while (coont)
	{
		
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			coont--;
		}
	}
}
int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
	return (mine[x - 1][y - 1] + mine[x-1][y] + mine[x - 1][y + 1]
		+ mine[x][y - 1] + mine[x][y + 1]
		+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0');
}
void ExpandBlank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* win)
{
	if (x <1 || x > ROW || y > COL|| y <1)
		return ;
	if (show[x][y] != '*')
		return ; 
	if (mine[x][y] == '1')
		return;
	int con = GetMineCount(mine, x, y);	
	show[x][y] = con + '0';
	(*win)++;
	if (con == 0)
	{
		for (int i = x-1; i <= x+1; i++)
		{
			for (int j = y -1; j <= y+ 1; j++)
			{
				ExpandBlank(mine, show, i, j,win);
			}
		}
	}	
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	time_t shi=time(NULL);
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			} 
			else
			{
				//该位置不是雷，就统计这个坐标周围有⼏个雷
				int count = GetMineCount(mine, x, y);
				if (count == 0)
				{
					ExpandBlank(mine, show, x, y,&win);
				}
				else
				{
					show[x][y] = count + '0';
					win++;
				}
				DisplayBoard(show, ROW, COL);
			}
		} 
		else
		{
		printf("坐标非法，重新输入\n");
		}
	} 
	time_t get = time(NULL);
	if(win == row * col - EASY_COUNT)
			{
		double  jian = difftime(get, shi);
				printf("恭喜你，排雷成功\n");
				printf("程序运行时间:%.0f 秒\n", jian);
				DisplayBoard(mine, ROW, COL);
			}
}
void InitBoard1(char board[ROWS1][COLS1], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void DisplayBoard1(char board[ROWS1][COLS1], int row, int col)
{
	printf("--------扫雷游戏-------\n");
	for (int i = 0; i <= row; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%2d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%2c ", board[i][j]);
		}
		printf("\n");
	}
}
void SetMine1(char board[ROWS1][COLS1], int row, int col)
{
	int coont = EASY_COUNT1;
	while (coont)
	{

		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			coont--;
		}
	}
}
int GetMineCount1(char mine[ROWS1][COLS1], int x, int y)
{
	return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
		+ mine[x][y - 1] + mine[x][y + 1]
		+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0');
}
void ExpandBlank1(char mine[ROWS1][COLS1], char show[ROWS1][COLS1], int x, int y, int* winn)
{
	if (x <1 || x > ROW1 || y > COL1 || y < 1)
		return;
	if (show[x][y] != '*')
		return;
	if (mine[x][y] == '1')
		return;
	int con = GetMineCount1(mine, x, y);
	show[x][y] = con + '0';
	(*winn)++;
	if (con == 0)
	{
		for (int i = x - 1; i <= x + 1; i++)
		{
			for (int j = y - 1; j <= y + 1; j++)
			{
				ExpandBlank1(mine, show, i, j, winn);
			}
		}
	}
}
void FindMine1(char mine[ROWS1][COLS1], char show[ROWS1][COLS1], int row, int col)
{
	int x = 0;
	int y = 0;
	int winn = 0;
	time_t shi = time(NULL);
	while (winn < row * col - EASY_COUNT1)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				DisplayBoard1(mine, ROW1, COL1);
				break;
			}
			else
			{
				//该位置不是雷，就统计这个坐标周围有⼏个雷
				int count = GetMineCount1(mine, x, y);
				if (count == 0)
				{
					ExpandBlank1(mine, show, x, y, &winn);
				}
				else
				{
					show[x][y] = count + '0';
					winn++;
				}
				DisplayBoard1(show, ROW1, COL1);
			}
		}
		else
		{
			printf("坐标非法，重新输入\n");
		}
	}

	time_t get = time(NULL);
	if (winn == row * col - EASY_COUNT1)
	{
		double  jian = difftime(get, shi);
		printf("恭喜你，排雷成功\n");
		printf("程序运行时间:%.0f 秒\n", jian);
		DisplayBoard1(mine, ROW1, COL1);
	}
}